import api from '@/store/api';
import i18n from '@/i18n';
import { usePerformanceMonitorStore } from '../PerformanceMonitor/PerformanceMonitorStore';
import PerformanceMonitor from '../../../views/SystemManagement/PerformanceMonitor/index';
const { t } = i18n.global as any;

export const useSystemStore = defineStore('SystemStore', () => {
  const systems = ref([] as any[]);
  const setSystemInfo = (data: {
    AssetTag: any;
    Description: any;
    BiosVersion: any;
    Name: any;
    Status: { Health: any; HealthRollup: any; State: any };
    Id: any;
    LocationIndicatorActive: any;
    Location: { PartLocation: { ServiceLabel: any } };
    Manufacturer: any;
    MemorySummary: {
      Status: { Health: any; HealthRollup: any; State: any };
    };
    Model: any;
    ProcessorSummary: {
      Count: any;
      Status: { Health: any; HealthRollup: any; State: any };
    };
    PowerState: any;
    SerialNumber: any;
    SubModel: any;
    SystemType: any;
  }) => {
    const performanceMonitorStore = usePerformanceMonitorStore();
    const tmp = performanceMonitorStore.hostStatus as {
      [index: string]: any;
    };
    const sysInfo = tmp?.SysInfo?.SysInfo[0];
    const system: { [index: string]: any } = {};
    system.assetTag = data.AssetTag;
    system.description = data.Description;
    system.firmwareVersion = data.BiosVersion;
    system.hardwareType = data.Name;
    system.health = data.Status?.Health;
    system.id = data.Id;
    system.locationIndicatorActive = data.LocationIndicatorActive;
    system.locationNumber = data.Location?.PartLocation?.ServiceLabel;
    system.manufacturer = data.Manufacturer;
    system.memorySummaryHealth = data.MemorySummary?.Status.Health;
    system.memorySummaryHealthRollup = data.MemorySummary?.Status?.HealthRollup;
    system.memorySummaryState = data.MemorySummary?.Status?.State;
    system.model = data.Model;
    system.processorSummaryCount = data.ProcessorSummary?.Count;
    system.processorSummaryHealth = data.ProcessorSummary?.Status?.Health;
    system.processorSummaryHealthRoll =
      data.ProcessorSummary?.Status.HealthRollup;
    system.processorSummaryState = data.ProcessorSummary?.Status?.State;
    system.powerState = data.PowerState;
    system.serialNumber = data.SerialNumber;
    system.healthRollup = data.Status?.HealthRollup;
    system.subModel = data.SubModel;
    system.statusState = data.Status?.State;
    system.systemType = data.SystemType;
    systems.value = [Object.assign({}, sysInfo, system)];
  };
  const getSystem = async () => {
    const performanceMonitorStore = usePerformanceMonitorStore();
    await performanceMonitorStore.getHostStatus().catch((err) => {
      console.log(err);
    });
    await api
      .get('/redfish/v1')
      .then((response) =>
        api.get(`${response.data.Systems['@odata.id']}/system`),
      )
      .then(({ data }) => setSystemInfo(data))
      .catch((error) => console.log(error));
  };
  const changeIdentifyLedState = async (ledState: any) => {
    return await api
      .patch('/redfish/v1/Systems/system', {
        LocationIndicatorActive: ledState,
      })
      .catch((error) => {
        setSystemInfo(systems.value[0]);
        console.log('error', error);
        if (ledState) {
          throw new Error(
            t('pageInventory.toast.errorEnableIdentifyLed') as string,
          );
        } else {
          throw new Error(
            t('pageInventory.toast.errorDisableIdentifyLed') as string,
          );
        }
      });
  };

  return { systems, setSystemInfo, getSystem, changeIdentifyLedState };
});
